#!/bin/bash
# 宝塔API的秘钥,从宝塔管理台上可以获取
api_sk="ANKCM0nXtgOk2nW3tjVodSadPNeVpJvo"
# 这里的端口号一定要跟服务器实际端口号匹配
baseUrl="http://127.0.0.1:8888/project/"
case "$1" in
  "stopJava")
    echo > /www/wwwlogs/java/springboot/${2}.log
    baseUrl="${baseUrl}java/stop_project"
    ;;
  "startJava")
    baseUrl="${baseUrl}java/start_project"
    ;;
  "restartJava")
    echo > /www/wwwlogs/java/springboot/${2}.log
    baseUrl="${baseUrl}java/restart_project"
    ;;
  *)
    echo "参数1错误: $1"
    exit 1;
    ;;
esac
# 生成请求时间戳
request_time=$(date +%s)

# 生成请求令牌
request_token=$(echo -n "${request_time}$(echo -n ${api_sk} | md5sum | cut -d ' ' -f 1)" | md5sum | cut -d ' ' -f 1)

# 构造表单数据
form_data="api_sk=${api_sk}&request_time=${request_time}&request_token=${request_token}&project_name=${2}"

# 发送 POST 请求
response=$(curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$form_data" "$baseUrl")

# 输出响应
echo $response
